package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.efounder.chat.activity.BaseActivity;
import com.efounder.recycleviewhelper.CommonAdapter;
import com.efounder.recycleviewhelper.base.ViewHolder;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.MarketSiteBean;
import com.pansoft.openplanet.manager.MarketExchangeSiteManager;
import com.utilcode.util.ReflectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 行情交易所选择界面
 * @author will
 */
public class MarketSelectActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private List<MarketSiteBean> siteList = new ArrayList<>();
    private MarketSiteBeanAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.op_activity_currencyandbourse);
        initView();
        initData();
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, MarketSelectActivity.class);
        context.startActivity(starter);
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(R.string.op_market_exchange_site);
        TextView tvSave = (TextView) findViewById(com.efounder.chat.R.id.tv_save);
        tvSave.setVisibility(View.VISIBLE);
        tvSave.setOnClickListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initData() {
        siteList = MarketExchangeSiteManager.getMarketSiteList();
        adapter = new MarketSiteBeanAdapter(this, R.layout.item_multi_language, siteList);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.tv_save){
            for (MarketSiteBean bean : siteList) {
                if (bean.isSelected()) {
                    MarketExchangeSiteManager.setCurrentMarketSite(bean.getUniqueKey());
                    MarketExchangeSiteManager.setCurrentMaketName(bean.getTitle());
                    try {
                        //跳转首页
                        String className = getResources().getString(R.string.from_group_backto_first);
                        ReflectUtils reflectUtils = ReflectUtils.reflect(className);
                        reflectUtils.method("start", this);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

    private class MarketSiteBeanAdapter extends CommonAdapter<MarketSiteBean> {
        public MarketSiteBeanAdapter(Context context, int layoutId, List<MarketSiteBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, MarketSiteBean siteBean, final int position) {
            TextView tvTitle;
            RadioButton rbLanguageSelect;

            tvTitle = holder.getView(R.id.tv_language_name);
            rbLanguageSelect = holder.getView(R.id.rb_language_select);

            tvTitle.setText(siteBean.getTitle());
            rbLanguageSelect.setChecked(siteBean.isSelected());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < siteList.size(); i++) {
                        MarketSiteBean siteBean = siteList.get(i);
                        if (position == i) {
                            siteBean.setSelected(true);
                        } else {
                            siteBean.setSelected(false);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }
}
